<template>
    <div class="admin-login-container" style="margin-top: -58px;">
      <!-- 粒子背景 -->
      <vue-particles
        color="#ffffff"
        :particleOpacity="0.7"
        :particlesNumber="80"
        shapeType="circle"
        :particleSize="4"
        linesColor="#ffffff"
        :linesWidth="1"
        :lineLinked="true"
        :lineOpacity="0.4"
        :linesDistance="150"
        :moveSpeed="3"
        :hoverEffect="true"
        hoverMode="grab"
        :clickEffect="true"
        clickMode="push"
        class="particles-bg"
      />
  
      <!-- 登录卡片 -->
      <div class="login-card">
        <div class="brand-header">
         
          <h1>CampusEasyBuy 管理后台</h1>
        </div>
  
        <el-form 
          ref="loginForm" 
          :model="form" 
          :rules="rules" 
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="管理员账号"
              prefix-icon="User"
              size="large"
              clearable
            />
          </el-form-item>
  
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              placeholder="登录密码"
              prefix-icon="Lock"
              size="large"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
  
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="login-btn"
              :loading="loading"
              native-type="submit"
            >
              {{ loading ? '登录中...' : '进入管理系统' }}
            </el-button>
          </el-form-item>
        </el-form>
  
        <div class="security-tips">
          <el-icon><Warning /></el-icon>
          <span>此为系统管理后台，仅限授权人员访问</span>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import axios from 'axios'
  
  const router = useRouter()
  const loginForm = ref(null)
  const loading = ref(false)
  
  const form = reactive({
    username: '',
    password: ''
  })
  
  const rules = reactive({
    username: [
      { required: true, message: '请输入管理员账号', trigger: 'blur' },
      { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入登录密码', trigger: 'blur' },
      { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
    ]
  })
  
  const handleLogin = async () => {
  try {
    await loginForm.value.validate()
    loading.value = true
    
    console.log('开始请求:', form)
    const response = await axios.post('http://localhost:8092/user/adminLogin', {
      username: form.username,
      password: form.password
    })
    console.log('响应数据:', response.data)

    if (response.data.code === 200) {
      console.log('登录成功，准备跳转')
      localStorage.setItem('adminToken', response.data.token)
      await router.push('/admin/main') // 添加await
      console.log('跳转完成')
      ElMessage.success('欢迎回来，管理员！')
    }
  } catch (error) {
    console.error('完整错误信息:', error)
    if (error.response) {
      console.log('错误响应:', error.response.data)
    }
    ElMessage.error(error.response?.data?.error || '请求异常')
  } finally {
    loading.value = false
  }
}
  </script>
  
  <style lang="scss" scoped>
  .admin-login-container {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
    position: relative;
    overflow: hidden;
  
    .particles-bg {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 1;
    }
  
    .login-card {
      position: relative;
      z-index: 2;
      width: 420px;
      padding: 40px;
      background: rgba(255, 255, 255, 0.95);
      border-radius: 16px;
      box-shadow: 0 12px 30px rgba(0, 0, 0, 0.2);
      transform: translateY(-10%);
  
      .brand-header {
        text-align: center;
        margin-bottom: 36px;
  
     
  
        h1 {
          font-size: 22px;
          color: #2c3e50;
          margin: 0;
          letter-spacing: 1px;
        }
      }
  
      :deep(.el-input__inner) {
        height: 48px;
        font-size: 16px;
        border-radius: 8px;
        transition: all 0.3s ease;
  
        &:focus {
          border-color: #3498db;
          box-shadow: 0 2px 8px rgba(52, 152, 219, 0.2);
        }
      }
  
      .login-btn {
        width: 100%;
        height: 48px;
        font-size: 16px;
        letter-spacing: 2px;
        background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
        border: none;
        border-radius: 8px;
        transition: all 0.3s ease;
  
        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
        }
      }
  
      .security-tips {
        margin-top: 24px;
        padding-top: 16px;
        border-top: 1px solid #eee;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #95a5a6;
        font-size: 12px;
  
        .el-icon {
          margin-right: 6px;
          font-size: 14px;
        }
      }
    }
  }
  
  @media (max-width: 480px) {
    .admin-login-container .login-card {
      width: 90%;
      padding: 30px;
      transform: none;
    }
  }
  </style>